import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Practice from '../views/Practice.vue'
import Problems from '../views/Problems.vue'
import Profile from '../views/Profile.vue'
import Membership from '../views/Membership.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import ProblemDetail from '../views/ProblemDetail.vue'
import PracticeDetail from '../views/PracticeDetail.vue'
import Moment from '../views/Moment.vue'
import Message from '@/views/Message.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/ai',
    component: () => import('@/views/AILayout.vue'),
    meta: { activeMenu: '/ai' },
    children: [
      {
        path: '',
        name: 'AI',
        component: () => import('@/views/AI.vue'),
        meta: { activeMenu: '/ai' }
      },
      {
        path: 'chat/:type',
        name: 'AIChat',
        component: () => import('@/views/AIChat.vue'),
        meta: { activeMenu: '/ai' }
      }
    ]
  },
  {
    path: '/roadmap',
    name: 'Resource',
    component: () => import('@/views/Resource.vue'),
    meta: { activeMenu: '/roadmap' }
  },
  {
    path: '/social',
    name: 'Social',
    component: () => import('@/views/Social.vue'),
    meta: { activeMenu: '/social' },
    children: [
      {
        path: '',
        name: 'Moment',
        component: () => import('@/views/Moment.vue'),
        meta: { activeMenu: '/social' }
      },
      {
        path: 'publish',
        name: 'MomentPublish',
        component: () => import('@/views/MomentPublish.vue'),
        meta: { 
          activeMenu: '/social',
          requiresAuth: true
        }
      },
      {
        path: ':id',
        name: 'MomentDetail',
        component: () => import('@/views/MomentDetail.vue'),
        meta: { activeMenu: '/social' }
      }
    ]
  },
  {
    path: '/problems',
    name: 'Problems',
    component: Problems,
    meta: { activeMenu: '/problems' },
    children: [
      {
        path: ':id',
        name: 'ProblemDetail',
        component: ProblemDetail,
        meta: { activeMenu: '/problems' }
      }
    ]
  },
  {
    path: '/practice',
    name: 'Practice',
    component: Practice,
    meta: { activeMenu: '/practice' },
    children: [
      {
        path: ':id',
        name: 'PracticeDetail',
        component: PracticeDetail,
        meta: { activeMenu: '/practice' }
      }
    ]
  },
  {
    path: '/record',
    name: 'PracticeRecord',
    component: () => import('@/views/PracticeRecord.vue'),
    meta: {
      title: '答题记录',
      requireAuth: true,
      activeMenu: '/practice'
    }
  },
  {
    path: '/profile/:id?',
    name: 'Profile',
    component: Profile,
    meta: { requiresAuth: true }
  },
  {
    path: '/membership',
    name: 'Membership',
    component: Membership,
    meta: { requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/feedback',
    name: 'Feedback',
    component: () => import('@/views/Feedback.vue')
  },
  {
    path: '/message',
    name: 'Message',
    component: Message,
    meta: { requiresAuth: true }
  },
  {
    path: '/likes',
    name: 'Likes',
    component: () => import('@/views/Likes.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/favorites',
    name: 'Favorites',
    component: () => import('@/views/Favorites.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/500',
    name: 'ServerError',
    component: () => import('@/views/500.vue')
  },
  {
    path: '/smart-qa',
    name: 'SmartQA',
    component: () => import('@/views/SmartQA.vue'),
    meta: {
      title: '智能问答',
      icon: 'ChatLineRound',
      requiresAuth: true
    }
  },
  {
    path: '*',
    name: 'NotFound',
    component: () => import('@/views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 添加错误处理
  if (!to.matched || to.matched.length === 0) {
    next('/')
    return
  }

  const token = localStorage.getItem('token')
  
  // 已登录用户不能访问登录页
  if (to.path === '/login' && token) {
    next('/')
    return
  }
  
  // 检查是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!token) {
      // 保存原目标路径
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
      return
    }
  }
  
  next()
})

export default router 